package com.example.zhunongzonghe.security.handler;//package com.example.zhunongzonghe.security.handler;

import com.alibaba.fastjson.JSON;
import com.example.zhunongzonghe.common.Result;
import com.example.zhunongzonghe.utils.WebUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录失败处理器
 */
@Component
public class LoginFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        Result result = new Result(false,"登录失败，请检查用户名或密码",400);
        String json = JSON.toJSONString(result);
        WebUtils.renderString(httpServletResponse,json);
    }
}